package 补充.网络编程;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class TCP网络程序客户端 {
    public static void main(String[] args) throws IOException {
        System.out.println("正在发送数据");
        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9191);
        OutputStream out = socket.getOutputStream();
        //发送数据
        out.write("服务端你好".getBytes());
        socket.shutdownOutput();
        InputStream inputStream = socket.getInputStream();
        byte[] buffer = new byte[1024];

        int len =0;
        //循环读取服务器端返回的数据
        while ((len = inputStream.read(buffer))!=-1) {
            System.out.println(new String(buffer, 0, len));
        }
        inputStream.close();
        out.close();
        socket.close();

    }
}
